package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import beans.LoteBean;

public class HibernateLoteDAO {
	private static HibernateLoteDAO instancia;
	private static SessionFactory sf;

	public static HibernateLoteDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateLoteDAO();
		}
		return instancia;
	}

	public LoteBean buscarLote(LoteBean ab) {
		Session session = sf.openSession();
		LoteBean lote = (LoteBean) session.get(LoteBean.class, ab.getIdLote());
		session.close();
		return lote;
	}

	public List<LoteBean> getLotes(int idSucursal) {
		Session session = sf.openSession();
		List<LoteBean> lotes = session
				.createQuery("from LoteBean a where a.idLote=?")
				.setInteger(0, idSucursal).list();
		return lotes;
	}

	public void update(LoteBean ab) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(ab);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public List<LoteBean> buscarUltimoLote() {
		Session session = sf.openSession();
		List<LoteBean> lotes = session.createQuery("from LoteBean").list();
		return lotes;
	}

	public void merge(LoteBean ab) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(ab);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
}
